#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
 struct ListNode {
     int val;
    struct ListNode *next;
};
typedef struct ListNode ListNode;
struct ListNode* removeNthFromEnd(struct ListNode* head, int n) {
    ListNode* newhead = (ListNode*)malloc(sizeof(ListNode));
    newhead->next = head;
    ListNode* fast = head;
    ListNode* slow = newhead;
    for (int i = 0; i < n; i++)
    {
        fast = fast->next;
    }
    while (fast)
    {
        fast = fast->next;
        slow = slow->next;
    }
    slow->next = slow->next->next;
    ListNode* ret = newhead->next;
    free(newhead);
    return ret;
}

struct ListNode* swapPairs(struct ListNode* head) {
    ListNode* newhead, * newtail;
    newhead = newtail = (ListNode*)malloc(sizeof(ListNode));
    ListNode* pcur = head;
    while (pcur)
    {
        if (pcur->next == NULL)
        {
            newtail->next = pcur;
            newtail = newtail->next;
            pcur = pcur->next;
        }

        else
        {
            ListNode* next = pcur->next->next;
            newtail->next = pcur->next;
            newtail->next->next = pcur;
            pcur = next;
            newtail = newtail->next->next;
        }
    }
    newtail->next = NULL;
    return newhead->next;
}

struct ListNode* rotateRight(struct ListNode* head, int k) {
    if (k == 0 || head == NULL || head->next == NULL)
        return head;
    ListNode* pcur = head;
    int count = 1;
    while (pcur->next)
    {
        count++;
        pcur = pcur->next;
    }
    pcur->next = head;
    k = count - k % count;
    for (int i = 0; i < k; i++)
    {
        pcur = pcur->next;
    }
    ListNode* ret = pcur->next;
    pcur->next = NULL;
    return ret;
}

struct ListNode* deleteDuplicates(struct ListNode* head) {
    if (head == NULL)
        return head;
    ListNode* newhead = (ListNode*)malloc(sizeof(ListNode));
    newhead->next = head;
    ListNode* pcur = newhead;
    while (pcur->next && pcur->next->next)
    {
        if (pcur->next->val == pcur->next->next->val)
        {
            int  x = pcur->next->val;
            while (pcur->next && pcur->next->val == x)
            {
                ListNode* del = pcur->next;
                pcur->next = pcur->next->next;
                free(del);
                del = NULL;
            }

        }
        else
        {
            pcur = pcur->next;
        }
    }
    ListNode* ret = newhead->next;
    free(newhead);
    newhead = NULL;
    return ret;
}
int main()
{
    ListNode* a, * b, * c, * d,*e;
    a = (ListNode*)malloc(sizeof(ListNode));
    b = (ListNode*)malloc(sizeof(ListNode));
    c = (ListNode*)malloc(sizeof(ListNode));
    d = (ListNode*)malloc(sizeof(ListNode));
    e = (ListNode*)malloc(sizeof(ListNode));
    a->val = 1;
    b->val = 2;
    c->val = 3;
    d->val = 4;
    e->val = 5;
    a->next = b;
    b->next = c;
    c->next = d;
    d->next = e;
    e->next = NULL;
    rotateRight(a, 2);
    return 0;
}